﻿
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Text;
using System.Xml;
using Gift.WebApp;
using Gift.DAL;
using Gift.Model;


namespace WebApp.admin.basic
{
    public partial class deliveryWay : BaseAdmin
    {
        // Fields
        tbDeliveryWay oDeliveryWay = new tbDeliveryWay();
      
        // Methods
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string pathAndQuery = HttpContext.Current.Request.Url.PathAndQuery;
            try
            {
                this.oDeliveryWay.deliveryWayName = this.txtDeliveryWayName.Value;
                this.oDeliveryWay.defaultLogisticsCompany = Convert.ToInt32(base.Request.Form["logisticsCompany"]);
                this.oDeliveryWay.sortOrder = Convert.ToInt32(this.txtSortOrder.Value);
                this.oDeliveryWay.introduce = this.txtaIntroduce.Value;
            
                if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
                {
                    this.oDeliveryWay.id = Convert.ToInt32(base.Request.QueryString["id"]);
                    if (BaseFactory.theDeliveryWay.Update(this.oDeliveryWay))
                    {
                        base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新成功！\");location.href=\"deliveryWayList.aspx\";</script>");
                    }
                    else
                    {
                        base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>selectIsExitItem(\"selectLogisticsCompany\",\"" + base.Request.Form["logisticsCompany"] + "\");alert(\"更新失败！\");</script>");
                    }
                }
                else
                {
                    this.oDeliveryWay.CompanyId = int.Parse( base.Administrator.CompanyId);
                    if (BaseFactory.theDeliveryWay.Add(this.oDeliveryWay) > 0)
                    {
                        base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加成功！\");location.href=\"deliveryWayList.aspx\";</script>");
                    }
                    else
                    {
                        base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>selectIsExitItem(\"selectLogisticsCompany\",\"" + base.Request.Form["logisticsCompany"] + "\");alert(\"添加失败！\");</script>");
                    }
                }
            }
            catch
            {
                if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>selectIsExitItem(\"selectLogisticsCompany\",\"" + base.Request.Form["logisticsCompany"] + "\");alert(\"更新失败！\");</script>");
                }
                else
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>selectIsExitItem(\"selectLogisticsCompany\",\"" + base.Request.Form["logisticsCompany"] + "\");alert(\"添加失败！\");</script>");
                }
            }
        }

        private void DeliveryWayBind()
        {
            try
            {
                tbDeliveryWay deliveryWayById = BaseFactory.theDeliveryWay.GetModel(Convert.ToInt32(base.Request.QueryString["id"]));
                if (deliveryWayById != null)
                {
                    this.txtDeliveryWayName.Value = deliveryWayById.deliveryWayName;
                    this.txtSortOrder.Value = deliveryWayById.sortOrder.ToString();
                    base.ClientScript.RegisterStartupScript(base.GetType(), "", "<script type=\"text/javascript\">selectIsExitItem(\"selectLogisticsCompany\",\"" + deliveryWayById.defaultLogisticsCompany + "\");</script>");
                    this.txtaIntroduce.Value = deliveryWayById.introduce;
                }
            }
            catch
            {
            }
        }

        protected StringBuilder GetTitle()
        {
            StringBuilder builder = new StringBuilder();
            try
            {
                if ((base.Request.QueryString["ot"] != null) && (base.Request.QueryString["id"] != null))
                {
                    builder.AppendLine("编辑方式");
                    return builder;
                }
                builder.AppendLine("添加方式");
            }
            catch
            {
            }
            return builder;
        }

        protected StringBuilder LogisticsCompanyBind()
        {
            StringBuilder builder = new StringBuilder();
            try
            {
                DataTable allLogisticsCompany = BaseFactory.theLogisticsCompany.GetList("").Tables[0];
                foreach (DataRow row in allLogisticsCompany.Rows)
                {
                    builder.AppendLine(string.Concat(new object[] { "<option value=\"", row["id"], "\">", row["logisticsCompanyName"], "</option>" }));
                }
                return builder;
            }
            catch
            {
                return builder;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //if ((HttpContext.Current.Session["admin_user"] == null) && (HttpContext.Current.Session["language_version"] == null))
            //{
            //    base.ClientScript.RegisterStartupScript(base.GetType(), "sJs", "top.location=\"login.html\";", true);
            //}
            if ((!base.IsPostBack && (base.Request.QueryString["ot"] != null)) && ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null)))
            {
                this.DeliveryWayBind();
            }
        }

    }
}